package com.example.duanlei417.mypro2.life_cycle;

import android.app.Activity;
import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.example.duanlei417.mypro2.R;
import com.example.duanlei417.mypro2.life_cycle.fragment.Tab1Fragment;
import com.example.duanlei417.mypro2.life_cycle.fragment.Tab2Fragment;
import com.example.duanlei417.mypro2.life_cycle.fragment.Tab3Fragment;
import com.example.duanlei417.mypro2.life_cycle.fragment.Tab4Fragment;

import java.util.ArrayList;
import java.util.List;

public class TestActivity extends Activity implements View.OnClickListener {

    private Tab1Fragment mTab1Fragment;
    private Tab2Fragment mTab2Fragment;
    private Tab3Fragment mTab3Fragment;
    private Tab4Fragment mTab4Fragment;

    private List<Fragment> mFragments = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lc_test);
        initView();
        initFragment();
    }

    private void initView() {
        findViewById(R.id.tv_tab1).setOnClickListener(this);
        findViewById(R.id.tv_tab2).setOnClickListener(this);
        findViewById(R.id.tv_tab3).setOnClickListener(this);
        findViewById(R.id.tv_tab4).setOnClickListener(this);
    }

    private void initFragment() {
        mTab1Fragment = new Tab1Fragment();
        addFragment(mTab1Fragment);
        showFragment(mTab1Fragment);
    }

    private void showFragment(Fragment fragment) {
        for (Fragment frag: mFragments) {
            //隐藏其它fragment
            if (frag != fragment) {
                getFragmentManager().beginTransaction().hide(frag).commit();
            }
        }
        getFragmentManager().beginTransaction().show(fragment).commit();
    }

    private void addFragment(Fragment fragment) {
        //判断该fragment是否添加，没有添加则添加
        if (!fragment.isAdded()) {
            getFragmentManager().beginTransaction().add(R.id.fl_content, fragment).commit();
            addToList(fragment);
        }
    }

    private void addToList(Fragment fragment) {
        if (fragment != null) {
            mFragments.add(fragment);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_tab1:
                if (mTab1Fragment == null) {
                    mTab1Fragment = new Tab1Fragment();
                }
                addFragment(mTab1Fragment);
                showFragment(mTab1Fragment);
                break;
            case R.id.tv_tab2:
                if (mTab2Fragment == null) {
                    mTab2Fragment = new Tab2Fragment();
                }
                addFragment(mTab2Fragment);
                showFragment(mTab2Fragment);
                break;
            case R.id.tv_tab3:
                if (mTab3Fragment == null) {
                    mTab3Fragment = new Tab3Fragment();
                }
                addFragment(mTab3Fragment);
                showFragment(mTab3Fragment);
                break;
            case R.id.tv_tab4:
                if (mTab4Fragment == null) {
                    mTab4Fragment = new Tab4Fragment();
                }
                addFragment(mTab4Fragment);
                showFragment(mTab4Fragment);
                break;
        }
    }
}
